package com.support.widget.network;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;

/**
 * Created by h2h on 2015/9/8.
 */
public class ProgressHelper {
	/**
	 * 包装OkHttpClient，用于下载文件的回调
	 * 
	 * @param client
	 *            待包装的OkHttpClient
	 * @param progressListener
	 *            进度回调接口
	 * @return 包装后的OkHttpClient，使用clone方法返回
	 */
	public static OkHttpClient addProgressResponseListener(OkHttpClient client, final ProgressResponseListener progressListener) {
		// 克隆
		OkHttpClient clone = client.clone();
		// 增加拦截器
		clone.networkInterceptors().add(new Interceptor() {
			@Override
			public Response intercept(Chain chain) throws IOException {
				// 拦截
				Response originalResponse = chain.proceed(chain.request());
				// 包装响应体并返回
				return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
			}
		});
		return clone;
	}

	/**
	 * 包装请求体用于上传文件的回调
	 * 
	 * @param requestBody
	 *            请求体RequestBody
	 * @param progressRequestListener
	 *            进度回调接口
	 * @return 包装后的进度回调请求体
	 */
	public static ProgressRequestBody addProgressRequestListener(RequestBody requestBody, ProgressRequestListener progressRequestListener) {
		// 包装请求体
		return new ProgressRequestBody(requestBody, progressRequestListener);
	}

}